package com.xbongbong.pro.message.pushset;/*/*/

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.message.domain.entity.PushSetEntity;
import com.xbongbong.pro.message.base.AbstractPushSet;
import com.xbongbong.pro.message.constant.PushSetConstant;
import com.xbongbong.pro.message.enums.PushObjectTypeEnum;
import com.xbongbong.pro.message.enums.PushTypeEnum;
import com.xbongbong.pro.message.model.PushSetModel;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
public class PaymentPlanPushSet extends AbstractPushSet{

    private String pushObject;

    private String pushTime;

    @Override
    public PushSetEntity toPushSetEntity(String pushObject, String pushTime, String attrEntity) {
        setPushObject(pushObject);
        setPushTime(pushTime);

        PushSetEntity pushSetEntity = new PushSetEntity();

        pushSetEntity.setAddTime(addTime);
        pushSetEntity.setAttrEntity(attrEntity);
        pushSetEntity.setCorpid(corpid);
        pushSetEntity.setDel(del);
        pushSetEntity.setEnable(enable);
        pushSetEntity.setId(id);
        pushSetEntity.setPushObject(pushObject);
        pushSetEntity.setPushTime(pushTime);
        pushSetEntity.setRefEntity(refEntity);
        pushSetEntity.setSubType(subType);
        pushSetEntity.setType(type);
        pushSetEntity.setUpdateTime(updateTime);

        return pushSetEntity;
    }

    @Override
    public PushSetEntity getPushSet(String corpid, Integer subType, PushSetModel pushSetModel) {
            Integer type = PushTypeEnum.PAYMENT_PLAN_ADD.getCode();

            Map<String,Object> param = new HashMap<>(16);
            param.put("corpid", corpid);
            param.put("type", type);
            param.put("subType", subType);
            List<PushSetEntity> pushSetList = pushSetModel.findEntitys(param);

            PushSetEntity pushSetEntity;
            if(pushSetList.isEmpty()){
                pushSetEntity = new PushSetEntity();
                pushSetEntity.setType(type);
                pushSetEntity.setSubType(subType);
                pushSetEntity.setEnable(0);
                JSONObject pushObject = new JSONObject();
                if (Objects.equals(PushTypeEnum.PAYMENT_PLAN_ADD.getSubCode(), subType)) {
                    pushSetEntity.setTitle(PushTypeEnum.PAYMENT_PLAN_ADD.getName());
                    pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.PAYMENT_PLAN_ADD));
                    //type
                    JSONArray typeArray = new JSONArray();
                    JSONObject object = new JSONObject();
                    object.put("name", PushObjectTypeEnum.MAINUSER.getName());
                    object.put("alias", PushObjectTypeEnum.MAINUSER.getAlias());
                    object.put("enable", 1);
                    typeArray.add(object);
                    JSONObject object1 = new JSONObject();
                    object1.put("name", PushObjectTypeEnum.COLLABORATOR.getName());
                    object1.put("alias", PushObjectTypeEnum.COLLABORATOR.getAlias());
                    object1.put("enable", 0);
                    typeArray.add(object1);
                    JSONObject object2 = new JSONObject();
                    object2.put("name", PushObjectTypeEnum.CONTRACT_SIGNING.getName());
                    object2.put("alias", PushObjectTypeEnum.CONTRACT_SIGNING.getAlias());
                    object2.put("enable", 1);
                    typeArray.add(object2);
                    pushObject.put("type", typeArray);
                    //manager
                    JSONObject managerObject = new JSONObject();
                    managerObject.put("enable", 0);
                    managerObject.put("value", new ArrayList<>());
                    pushObject.put("manager", managerObject);
                    //dep
                    JSONObject depObject = new JSONObject();
                    depObject.put("enable", 0);
                    depObject.put("value", new ArrayList<>());
                    pushObject.put("dep", depObject);
                    //role
                    JSONObject roleObject = new JSONObject();
                    roleObject.put("enable", 0);
                    roleObject.put("value", new ArrayList<>());
                    pushObject.put("role", roleObject);
                    //user
                    JSONObject userObject = new JSONObject();
                    userObject.put("enable", 0);
                    userObject.put("value", new ArrayList<>());
                    pushObject.put("user", userObject);
                }
                pushSetEntity.setPushObject(pushObject.toJSONString());
            } else {
                pushSetEntity = pushSetList.get(0);
                if (Objects.equals(PushTypeEnum.PAYMENT_PLAN_ADD.getSubCode(), subType)) {
                    pushSetEntity.setTitle(PushTypeEnum.PAYMENT_PLAN_ADD.getName());
                    pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.PAYMENT_PLAN_ADD));
                }
            }
            return pushSetEntity;

    }

    @Override
    public PushSetEntity getPushSet(String corpid, String attr, PushSetModel pushSetModel) {
        return null;
    }

    @Override
    public List<PushSetEntity> getPushSet(String corpid, List<String> attrList, PushSetModel pushSetModel, Long templateId) {
        return null;
    }

    @Override
    public JSONObject getTimeJson(JSONObject jsonObject) {
        return null;
    }
}
